<?php
/***************************************************************************
*                                register.php
 *
 *   Begin date           	: 
 *   Copyright            	: 
 *   Email                	: 
 ***************************************************************************/
defined('PHP5_PHP') or die("Application is stopping!!!");
//checkLogin();
require_once($php5RootPath . "/classes/customer.class.php");
$row = new php5Customer($php5DB);
$task 		= php5GetParam($_REQUEST, 'task', '');
$type_id = $id 		= php5GetParam($_REQUEST, 'id', '');
$row->load($id);
if(!$row->name) {
	return;
}

$smarty->setCompileDir(sprintf($php5RootTempalteCPath, ''));
$tpl = sprintf($php5TemplateFile, $language, 'customer', 'edit.tpl');
if(!is_file($tpl )) {
	die("can not find template");
}


//check task
$msg = '';
$noerr = 1;
switch($task)
{
	case 'update':
		$row->bind( $_POST );
		if(!trim($row->name)) {
			$noerr = 0;
			$msg = sprintf($lang['_BLANK_MSG_'], 'Name');
		}
		if($noerr && !trim($row->company)) {
			$noerr = 0;
			$msg = sprintf($lang['_BLANK_MSG_'], 'Company');
		}
		if($noerr && !trim($row->email)) {
			$noerr = 0;
			$msg = sprintf($lang['_BLANK_MSG_'], 'Email');
		}
		if($noerr &&  preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/", $row->email )==false) {
			$noerr = 0;
			$msg = sprintf($lang['_INVALID_MSG_'], 'Email');
			break;
		}
		
		$where = array();
		$where[] = "email = '".mysql_real_escape_string($row->email)."'";
		$where[] = "user_id = '".$php5Session->getVar("user_id")."'";
		$where[] = "id != '$id'";
		
		$query = "SELECT COUNT(*) "
		. "\n FROM #__customers "
		. ( count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "" )
		;
		
		$php5DB->setQuery( $query );
		$count = $php5DB->loadResult();	
		if($noerr &&  $count) {
			$noerr = 0;
			$msg = $lang['_MSS_003_'];
			break;
		}		
		if($noerr && !trim($row->phone)) {
			$noerr = 0;
			$msg = sprintf($lang['_BLANK_MSG_'], 'Phone');
		}
		if($noerr && preg_match("/^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/", $row->phone )==false) {
			$noerr = 0;
			$msg = sprintf($lang['_INVALID_MSG_'], 'Phone');
			break;
		}						
		if($noerr && !trim($row->address)) {
			$noerr = 0;
			$msg = sprintf($lang['_BLANK_MSG_'], 'Address');
		}	
				
		if($noerr) {
			$row->user_id = $php5Session->getVar("user_id");
			if (!$row->store()) {
				$msg = $msgAlert = "Error!!! Please try again!!!";		
			} else {
				$msg = $msgAlert = $lang['_UPDATE_MSG_'];
				$redirect = sefBuild($php5WebPath, 'index.php?o=customer&m=main', 1);
			}
		}
		break;
	default:
		break;	
		
}
$smarty->assign('msg', '');
if($msg) {
	$smarty->assign('msg', $msg);
}
//assign
$smarty->assign('name', $row->name);
$smarty->assign('company', $row->company);
$smarty->assign('email', $row->email);
$smarty->assign('phone', $row->phone);
$smarty->assign('address', $row->address);
$smarty->assign('checkAll', 1);

$smarty->assign('type_id', $type_id);
$smarty->assign('link_import', sefBuild($php5WebPath, 'index.php?o=import_newsletter_users', 1));
$smarty->assign('link_export', sefBuild($php5WebPath, 'index.php?o=export_newsletter_users', 1)."?&type=$type_id");

$smarty->assign('link_return', sefBuild($php5WebPath, 'index.php?o=customer&m=main', 1));
$smarty->assign('action', sefBuild($php5WebPath, 'index.php?o=customer&m=edit&task=update&id='.$id, 1));
$smarty->assign('main', $smarty->fetch($tpl));

return;
